<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>添加魔法</title>
    <style>
        .two-part-label{
            width: 200px !important;
        }
    </style>
</head>
<body>
<form class="layui-form" lay-filter="layui-form">
    <div class="mainBox">
        <div class="main-container">
            <div class="layui-form-item">
                <label class="layui-form-label">
                    <text style="color:red;">*</text>
                    日期名称</label>
                <div class="layui-input-block">
                    <input type="text" name="date_name" placeholder="请输入日期名称" class="layui-input" lay-verify="required"
                           autocomplete="off" readonly>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">
                    <text title="必填项" style="color:red;">*</text>
                    添加魔法</label>
                <div class="layui-input-block">
                    <div class="xmSelect"
                         data-name="magic_id"
                         data-sourceType="route"
                         data-source="/admin.sky.magicData/index"
                         data-textField="magic_name"
                         data-valueField="id"
                         data-paging="true"
                         data-radio="false"
                         data-layVerify="required"
                         data-direction="down"
                    ></div>
                </div>
            </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="laytp-btn laytp-btn-primary laytp-btn-sm" lay-submit="" lay-filter="add">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button type="reset" class="laytp-btn laytp-btn-sm">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>
<script>
    if(localStorage.getItem("staticDomain")){
        document.write("<link rel='stylesheet' href='" + localStorage.getItem("staticDomain") + "/component/layui/css/layui.css?v=" + localStorage.getItem("version") + "'>");
        document.write("<script src='" + localStorage.getItem("staticDomain") + "/component/layui/layui.js?v="+localStorage.getItem("version")+"'><\/script>");
        document.write("<script src='" + localStorage.getItem("staticDomain") + "/component/laytp/layuiConfig.js?v="+localStorage.getItem("version")+"'><\/script>");
    }else{
        document.write("<link rel='stylesheet' href='/static/component/layui/css/layui.css?v=" + localStorage.getItem("version") + "'>");
        document.write("<script src='/static/component/layui/layui.js?v="+localStorage.getItem("version")+"'><\/script>");
        document.write("<script src='/static/component/laytp/layuiConfig.js?v="+localStorage.getItem("version")+"'><\/script>");
    }
</script>
<script>
    var data = {};
    var layer, $, laydate;
    layui.use(['layer', 'form', 'jquery', 'laydate'], function () {
        layer = layui.layer;
        $ = layui.jquery;
        laydate = layui.laydate;
        var new_date = new Date();
        loding_date(new_date, data);
    })
    //日历插件调用方法
    function loding_date(date_value, data) {
        laydate.render({
            elem: '#test-date'
            , type: 'date'
            , min : getNowFormatDate()
            , theme: 'grid'
            , position: 'static'
            , range: false
            , value: date_value
            , isInitValue: false // 2.3.0新增
            , calendar: false
            , btns: false
            , ready: function (value) {
                hide_this(value);
            }
            , done: function (value, date, endDate) {
                add_new(value, data)
            }
            , change: function (value, date) {
                hide_this(date);
            }
            , mark: data // 选中的所有日期赋值, 默认赋值格式为json 如：{'2021-03-03':''}
        });

        $('#magic_date').val(Object.keys(data).join(','))
    }

    function getNowFormatDate() {
        var date = new Date();
        var seperator1 = "-";
        var seperator2 = ":";
        var month = date.getMonth() + 1;
        var strDate = date.getDate();
        if (month >= 1 && month <= 9) {
            month = "0" + month;
        }
        if (strDate >= 0 && strDate <= 9) {
            strDate = "0" + strDate;
        }
        var currentdate = date.getFullYear() + seperator1 + month
            + seperator1 + strDate + " " + date.getHours() + seperator2
            + date.getMinutes() + seperator2 + date.getSeconds();
        return currentdate;
    }

    function hide_this(value) {
        var mm = value.year + '-' + value.month + '-' + value.date;
        $('.laydate-theme-grid table tbody').find('[lay-ymd="' + mm + '"]').removeClass('layui-this');
    }
    //定义添加/编辑标注方法
    function add_new(obj_date, markJson) {
        $('#test-date').html(''); // 先清除原先表格信息
        if (markJson.hasOwnProperty(obj_date)) {
            delete markJson[obj_date]; // 删除取消日期
        } else {
            // 新增选择日期
            markJson[obj_date] = ''
        }
        loding_date(obj_date, markJson);// 重新创建个新的日期表格
    }
</script>
<script>
    layui.use(['laytp','xmSelect'], function () {
        layui.form.on('submit(add)', function(data){
            var btnAnim = layui.button.load({elem:$(this)});
            facade.ajax({
                route:'/admin.sky.magic/add',
                data : data.field
            }).done(function(res){
                if(res.code === 0){
                    parent.layui.layer.close(parent.layui.layer.getFrameIndex(window.name));//关闭当前页
                    parent.funController.tableRender();
                    parent.parent.renderMenu();//重新渲染菜单
                }
                btnAnim.stop();
            }).fail(function(){
                btnAnim.stop();
            });
            return false;
        });
    });
</script>
</body>
</html>